<template>
  <div class="AdendDialog">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="字典名称" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="描述">
            <el-input v-model="ruleForm.description" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="排序">
            <el-input v-model="ruleForm.sort" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >编辑</el-button
        >
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: ["row",'dialog'],
  data() {
    return {
      ruleForm: {
        name: "",
        description: "",
        sort: "",
        id: ""
      },
      rules: {
        name: [{ required: true, message: "请选择活动区域", trigger: "blur" }]
      }
    };
  },
  watch: {
    dialog: {
      handler() {
        if(this.dialog){
          if (this.$refs["ruleForm"]) {
          this.$refs["ruleForm"].resetFields();
        }
        Object.keys(this.ruleForm).forEach(key => {
          this.ruleForm[key] =  this.row[key];
        });
        }
        
      },
      immediate: true, // immediate选项可以开启首次赋值监听
      deep: true
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$server.editDict(this.ruleForm).then(res => {
            const val = {
              flag: true
            };
            this.$emit("chang", val);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      const val = {
        flag: false
      };
      this.$emit("chang", val);
    }
  }
};
</script>
<style lang="scss" scoped>
.AdendDialog {
}
</style>
